layui.define(['jquery', 'element','layer', ], function (exports) {

    var $ = layui.$,
        layer = layui.layer,
        element = layer.element;

    wsiteapi  = new function (env) {

        this.envConfig = {};

        this.config = function (env) {
            var config = {
                dev: {
                    url: 'http://localhost:5000/',
                    limits: [10, 20, 50, 100],
                    defaultLimit: 10
                },
                test: {
                    url: 'http://localhost:8080/',
                    limits: [10, 20, 50, 100],
                    defaultLimit: 10
                },
                prod: {
                    url: 'http://localhost:8080/',
                    limits: [10, 20, 50, 100],
                    defaultLimit: 10
                },
            };
            if (env == undefined) {
                return config;
            } else {
                return config[env];
            }
        };
        this.init = function (env) {
            this.envConfig = wsiteapi.config(env == undefined ? 'dev' : env);
        };

        this.login = function (login_param) {
            console.log('wsite login param', login_param);
            var url = wsiteapi.envConfig.url + 'auth/login',
                successCallback = function (data, status) {
                    console.log('server response success', data, status);
                },
                failedCallable = function (err, status) {
                    console.log('server error:', err, status);

                };
            wsiteapi.sendDate2Server(url, login_param, successCallback, failedCallable);

        };
        this.logout = function (logout_param) {
            $.post(this.envConfig.url + '/auth/logout', logout_param, function (data, status) {
                console.log('$.post response', data, status);
            });

        };

        this.modifyFilm = function (film_param, successCallable, failCallable) {
            var url = this.envConfig.url + 'seed/modify/film';
            this.sendDate2Server(url, film_param, successCallable, failCallable);

        };

        this.sendDate2Server = function (url, param, successCallable, failCallabel) {
            console.log('sendDate2Server', url, param);
            $.ajax({
                url: url,
                datatype: 'json',
                headers: {
                    "Content-Type": "application/json;charset=utf-8"
                },
                contentType: 'application/json; charset=utf-8',
                type: 'POST',
                timeout: 3000,
                data: JSON.stringify(param),
                success: successCallable,
                error: failCallabel
            });

        };


    };

    exports('wsiteapi', wsiteapi);

});
